/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cafe.Controller;

import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import hapax.TemplateException;
import hapax.TemplateLoader;
import hapax.TemplateResourceLoader;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author TieuHan
 */

/*
 Tao Map luu lai cac Template da load len
 */
public class TemplateCollection {

    private static TemplateCollection _templateCollection = null;
    private static TemplateDataDictionary _templateDataDictionary;
    Map<String, Template> templateList;
    private TemplateLoader templateLoader;

    private TemplateCollection() {
        templateList = new HashMap<String, Template>();
        templateLoader = TemplateResourceLoader.create("cafe/");
        _templateDataDictionary = TemplateDictionary.create();
    }

    public static synchronized TemplateCollection getInstance() {
        if (_templateCollection == null) {
            _templateCollection = new TemplateCollection();
        }
        return _templateCollection;
    }

    public TemplateDataDictionary getTemplateDataDictionary() {
        return _templateDataDictionary;
    }

    public Template getTemplate(String path) throws TemplateException {
        if (templateList.containsKey(path)) {
            return templateList.get(path);
        } else {
            Template template = templateLoader.getTemplate(path);
            templateList.put(path, template);
            return template;
        }
    }
}
